package com.yichen.rent.controller;

import com.alibaba.fastjson.JSON;
import com.yichen.rent.pojo.Desc;
import com.yichen.rent.response.HouseDescResponse;
import com.yichen.rent.service.DescService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/desc")
public class DescController {

    @Autowired
    private DescService descService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", descToHouseDescResponseList(descService.findAll()));
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", descToHouseDescResponse(descService.findById(id)));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Desc> pageList = descService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<HouseDescResponse>(pageList.getTotalElements(), descToHouseDescResponseList(pageList.getContent())));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", descToHouseDescResponseList(descService.findSearch(searchMap)));
    }

    /**
     * 增加
     *
     * @param houseDescResponse
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody HouseDescResponse houseDescResponse) {
        descService.add(houseDescResponseToDesc(houseDescResponse));
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param houseDescResponse
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody HouseDescResponse houseDescResponse, @PathVariable String id) {
		houseDescResponse.setId(id);
        descService.update(houseDescResponseToDesc(houseDescResponse));
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        descService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    private List<HouseDescResponse> descToHouseDescResponseList(List<Desc> descList) {
        List<HouseDescResponse> list = new ArrayList<>();
        for (Desc desc : descList) {
            HouseDescResponse houseDescResponse = new HouseDescResponse();
            houseDescResponse.setHouse_id(desc.getHouse_id());
            houseDescResponse.setDescriptor(desc.getDescriptor());
            houseDescResponse.setId(desc.getId());
            List<String> images = JSON.parseArray(desc.getPicture(), String.class);
            houseDescResponse.setPicture(images);
            list.add(houseDescResponse);
        }
        return list;
    }

    private HouseDescResponse descToHouseDescResponse(Desc desc) {
        HouseDescResponse houseDescResponse = new HouseDescResponse();
        houseDescResponse.setHouse_id(desc.getHouse_id());
        houseDescResponse.setDescriptor(desc.getDescriptor());
        houseDescResponse.setId(desc.getId());
        List<String> images = JSON.parseArray(desc.getPicture(), String.class);
        houseDescResponse.setPicture(images);

        return houseDescResponse;
    }

    private Desc houseDescResponseToDesc(HouseDescResponse houseDescResponse) {
    	Desc desc = new Desc();
    	desc.setId(houseDescResponse.getId());
    	desc.setHouse_id(houseDescResponse.getHouse_id());
    	desc.setDescriptor(houseDescResponse.getDescriptor());
    	desc.setPicture(JSON.toJSONString(houseDescResponse.getPicture()));
    	return desc;
	}
}
